/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.orm.testdesign;

import java.util.HashMap;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_test_designtree;
import org.testis.orm.classes.T_test_stepcontainer;
import org.testis.orm.manager.TestisDbPersistentManager;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined;
import org.testis.orm.refinedclasses.TestPlanRefined;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;

/**
 * 
 * @author nurhat
 */
public class TestPlanDataHandler extends TestPartDataHandler {

    @Override
    public TestDesignTreeNode operationAdd(HashMap<String, Object> params,
	    TestDesignTreeNode parentNode, int childIndex) {
	TestDesignTreeNode newNode = null;
	String name = (String) params.get("name");
	String description = (String) params.get("description");
	String browserSpeed = (String) params.get("browserSpeed");
	String browserType = (String) params.get("browserType");
	String deviceIp = (String) params.get("deviceIp");
	String pc = (String) params.get("pc");
	String dut = (String) params.get("dut");
	String owner = (String) params.get("owner");
	String languageId = (String) params.get("languageId");
	String profileId = (String) params.get("profileId");
	String testBedId = (String) params.get("testBedId");
	String timeout = (String) params.get("timeout");
	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		T_test_stepcontainer container = new T_test_stepcontainer();
		container.setName(name);
		container.setDescription(description);
		container.setContainerTypeId(TestDesign
			.getDesignPartTypeIdByName("TestPlan"));
		container.save();
		T_test_designtree obj = new T_test_designtree();
		obj.save();
		TestDesignTreeRefined designTree = TestDesignTreeRefined
			.getDesignTree(obj);
		designTree.getTreeReference().setParent(
			parentNode.getReference().getTreeReference());
		designTree.getTreeReference().setElementId(container.getId());
		designTree.getTreeReference().setTypeId(
			container.getContainerTypeId());
		designTree.getTreeReference().save();

		if (owner != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(),
			    TestDesign.m_owners.get(owner).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(), 0);
		}
		if (dut != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceId.name(),
			    TestDesign.getDutById(dut).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceId.name(), null);
		}
		designTree
			.setParameterValue(
				TestDesignTreeParameters.SuccessPercentage
					.name(), null);
		designTree.setParameterValue(
			TestDesignTreeParameters.NicId.name(), null);
		if (timeout != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.Timeout.name(),
			    Integer.parseInt(timeout));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.Timeout.name(), null);
		}
		if (browserSpeed != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserSpeed.name(),
			    Integer.parseInt(browserSpeed));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserSpeed.name(), null);
		}
		if (languageId != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.LanguageId.name(),
			    Integer.parseInt(languageId));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.LanguageId.name(), null);
		}
		if (profileId != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ProfileId.name(),
			    Integer.parseInt(profileId));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ProfileId.name(), null);
		}
		if (testBedId != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.TestBedId.name(),
			    Integer.parseInt(testBedId));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.TestBedId.name(), null);
		}

		if (deviceIp != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), deviceIp);
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), null);
		}
		if (!browserType.equals("*")) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserType.name(),
			    browserType);
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserType.name(), null);
		}

		if (!pc.equals("")) {
		    designTree.setParameterValue(TestDesignTreeParameters.PcId
			    .name(), TestDesign.getPcByName(pc).getId());

		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.PcId.name(), null);
		}
		designTree.getTreeReference().save();
		int place = TestDesign.setRankOfAdd(parentNode,
			designTree.getTreeReference(), childIndex);
		designTree.getTreeReference().save();
		t.commit();

		TestPlanRefined result = new TestPlanRefined(container);
		newNode = TestDesignTreeNode.getNode(designTree, result);
		if (place == -1) {
		    parentNode.addChild(newNode);
		} else {
		    parentNode.addChild(place, newNode);
		    newNode.setPlace(place);
		}
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
		newNode = null;
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	    newNode = null;
	}
	return newNode;
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
	    TestDesignTreeNode node) {
	String name = (String) params.get("name");
	String description = (String) params.get("description");

	// Dynamic Parameters
	String browserSpeed = (String) params.get("browserSpeed");
	String browserType = (String) params.get("browserType");
	String deviceIp = (String) params.get("deviceIp");
	String dut = (String) params.get("dut");
	String pc = (String) params.get("pc");
	String languageId = (String) params.get("languageId");
	String profileId = (String) params.get("profileId");
	String testBedId = (String) params.get("testBedId");
	String dynamicSaveOnly = (String) params.get("dynamicSaveOnly");
	String timeout = (String) params.get("timeout");
	// End Dynamic Parameters

	TestDesignTreeRefined designTree = node.getReference();
	TestPlanRefined container = (TestPlanRefined) node.getObject();
	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		if (dynamicSaveOnly == null) {
		    container.getReference().setName(name);
		    container.getReference().setDescription(description);
		    container.getReference().save();
		}
		if (dut != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceId.name(),
			    TestDesign.getDutById(dut).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceId.name(), null);
		}
		if (timeout != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.Timeout.name(),
			    Integer.parseInt(timeout));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.Timeout.name(), null);
		}
		if (browserSpeed != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserSpeed.name(),
			    Integer.parseInt(browserSpeed));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserSpeed.name(), null);
		}
		if (languageId != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.LanguageId.name(),
			    Integer.parseInt(languageId));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.LanguageId.name(), null);
		}
		if (profileId != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ProfileId.name(),
			    Integer.parseInt(profileId));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.ProfileId.name(), null);
		}
		if (testBedId != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.TestBedId.name(),
			    Integer.parseInt(testBedId));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.TestBedId.name(), null);
		}
		if (deviceIp != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), deviceIp);
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.DeviceIp.name(), null);
		}
		if (!browserType.equals("*")) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserType.name(),
			    browserType);
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.BrowserType.name(), null);
		}

		if (!pc.equals("")) {
		    designTree.setParameterValue(TestDesignTreeParameters.PcId
			    .name(), TestDesign.getPcByName(pc).getId());

		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.PcId.name(), null);
		}
		designTree.getTreeReference().save();
		t.commit();
		return true;
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
	return false;
    }
}
